package com.prj.dy.datamgr
{
	import com.prj.dy.common.DYDispatcher;
	import com.prj.dy.datamgr.undo.DYCmdMgr;
	import com.prj.dy.datamgr.undo.DYXmlDataChangeEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	import mx.messaging.channels.StreamingAMFChannel;
		
	[Bindable]
	public class DYPreference extends EventDispatcher
	{
		
		// The Singleton instance.
		protected static var instance : DYPreference = null; 
		
		// Message Constants
		protected const SINGLETON_MSG	: String = "DYAppPref Singleton already constructed!";		
		
		
		public function DYPreference(target:IEventDispatcher=null)
		{
			super(target);
			
			dyCmdMgr = new DYCmdMgr;
			
			if (instance != null) 
				throw Error(SINGLETON_MSG);	
		}
		
		public static function getInstance():DYPreference
		{
			if (instance == null) 
				instance = new DYPreference( );
			
			return instance;
		}
				
		
		
		//////////////////// begin get set for://////////////////////////////////		
		public static const PROJECT_RESET :String = "ProjectReset";  
				
		private var _xmlOfProject:XML = null;
		
		[Bindable(event="ProjectReset")]
		public function get xmlOfProject():XML
		{
			return _xmlOfProject;
		}
		
		public function set xmlOfProject(value:XML):void
		{
			_xmlOfProject = value;
			this.onProjectReset();
			this.dispatchEvent(new Event("ProjectReset"));
		}				
		////////////////// end get set //////////////////////////////////////////		

		// Project 重置时数据数组作更改
		protected function onProjectReset():void
		{			
			arrCollOfPgLayers = new ArrayCollection;
			
			var i:int = 1;
			var pgLayer:PgLayerModel = new PgLayerModel;	
			var xmlListPages:XMLList = xmlOfProject.doc.album.inner_pages.page;
			for each(var xmlPage:XML in xmlListPages)
			{
				
				var pg:PgModel = new PgModel(xmlPage);
				
				pgLayer.arrCollOfPgs.addItem(pg);
				
				if (i % numPagesToEdit == 0)
				{
					arrCollOfPgLayers.addItem(pgLayer);
					pgLayer = new PgLayerModel;
				}
					
				i++;
			}							
		}
		

		private var _numPagesToEdit:int = 1;

		public function get numPagesToEdit():int
		{
			return _numPagesToEdit;
		}

		public function set numPagesToEdit(value:int):void
		{
			_numPagesToEdit = value;
			
			curSelectedPgLayerIndex = -1;
			this.onProjectReset();	
			curSelectedPgLayerIndex = 0;
		}
		
		
		
		
		//////////////////// begin get set for://////////////////////////////////
		private var _arrCollOfPgLayers:ArrayCollection;

		public function get arrCollOfPgLayers():ArrayCollection
		{
			return _arrCollOfPgLayers;
		}

		public function set arrCollOfPgLayers(value:ArrayCollection):void
		{
			_arrCollOfPgLayers = value;
		}
		////////////////// end get set //////////////////////////////////////////

		
		//////////////////// begin get set for:////////////////////////////////// 		
		private var _curSelectedPgLayerIndex:int = -1;

		public function get curSelectedPgLayerIndex():int
		{
			return _curSelectedPgLayerIndex;
		}

		public function set curSelectedPgLayerIndex(value:int):void
		{
			_curSelectedPgLayerIndex = value;
			if (curSelectedPgLayerIndex >= 0)
				curSelectedPgLayer = arrCollOfPgLayers.getItemAt(curSelectedPgLayerIndex) as PgLayerModel; 

		}
		////////////////// end get set //////////////////////////////////////////		
		

		//////////////////// begin get set for://////////////////////////////////		
		private var _curSelectedPgLayer:PgLayerModel = null;

		public function get curSelectedPgLayer():PgLayerModel
		{
			return _curSelectedPgLayer;
		}

		protected function set curSelectedPgLayer(value:PgLayerModel):void
		{
			_curSelectedPgLayer = value;
		}
		////////////////// end get set //////////////////////////////////////////
		
		
		private var _dyCmdMgr:DYCmdMgr = null;

		public function get dyCmdMgr():DYCmdMgr
		{
			return _dyCmdMgr;
		}

		public function set dyCmdMgr(value:DYCmdMgr):void
		{
			_dyCmdMgr = value;
		}
		
		
		
		
		public function dispatchXmlDataChangeEvent(xmlOrg:XML, xmlNew:XML, propertyName:String):void
		{
			DYDispatcher.getInstance().dispatchEvent(new DYXmlDataChangeEvent(this, xmlOrg, xmlNew, propertyName));
		}
		
		
	}
}

